﻿using BaseUi.ViewModel;
using HandyControl.Controls;
using KeyBoard.WPF.Behavior;
using Microsoft.Xaml.Behaviors.Input;
using Ping9719.WpfEx;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BaseUi
{
    public partial class TextBoxNumber : HandyControl.Controls.TextBox
    {
        NumericKeyboardBehavior keyBoardTrigger = new NumericKeyboardBehavior();

        public TextBoxNumber()
        {
            //初始化控件时绑定小键盘 keyBoardTrigger
            keyBoardTrigger.UCBackground = new SolidColorBrush(Colors.AliceBlue);
            Microsoft.Xaml.Behaviors.Interaction.GetBehaviors(this).Add(keyBoardTrigger);

            this.Loaded += new RoutedEventHandler(TextBoxNumber_Loaded);
            this.KeyDown += new KeyEventHandler(TextBoxNumber_KeyDown);
            this.GotKeyboardFocus += TextBoxNumber_GotKeyboardFocus;

        }

        public bool IsSuccessSet = false;
        private void TextBoxNumber_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            if (IsSuccessSet)
            {
                IsSuccessSet = false;
                try
                {
                    Microsoft.Xaml.Behaviors.Interaction.GetBehaviors(this).Add(keyBoardTrigger);
                }
                catch { }
            }
        }

        private void TextBoxNumber_Loaded(object sender, RoutedEventArgs e)
        {
            //this.Text = MaxValue.ToString();
        }

        /// <summary>
        /// 控件禁用触摸屏键盘
        /// </summary>
        /// <returns></returns>
        protected override AutomationPeer OnCreateAutomationPeer()
        {
            return new FrameworkElementAutomationPeer(this);
        }

        private void TextBoxNumber_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                //值设置正确的情况
                if (MinValue < MaxValue)
                {
                    try
                    {
                        //设置值为int的情况
                        if (string.IsNullOrEmpty(ValueType) || ValueType == "int")
                        {
                            var value = Convert.ToUInt32(this.Text);
                            this.Text = value.ToString();

                            //上下限校验不通过
                            if (value < MinValue || value > MaxValue)
                            {
                                this.Foreground = Brushes.Red;
                                Growl.Warning($"MinValue:{MinValue},MaxValue:{MaxValue}。（You Should Set {MinValue}~{MaxValue}）");
                                Keyboard.ClearFocus();
                                e.Handled = true;
                            }
                            else
                            {
                                IsSuccessSet = true;
                                Microsoft.Xaml.Behaviors.Interaction.GetBehaviors(this).Remove(keyBoardTrigger);
                                this.Foreground = Brushes.Black;
                                Keyboard.ClearFocus();
                            }
                        }
                        //设置值为double的情况
                        else
                        {
                            var value = Convert.ToDouble(this.Text);
                            //上下限校验不通过
                            if (value < MinValue || value > MaxValue)
                            {
                                this.Foreground = Brushes.Red;
                                Growl.Warning($"MinValue:{MinValue},MaxValue:{MaxValue}。（You Should Set {MinValue}~{MaxValue}）");
                                Keyboard.ClearFocus();
                                e.Handled = true;
                            }
                            else
                            {
                                IsSuccessSet = true;
                                //暂时屏蔽小键盘
                                Microsoft.Xaml.Behaviors.Interaction.GetBehaviors(this).Remove(keyBoardTrigger);
                                this.Foreground = Brushes.Black;
                                Keyboard.ClearFocus();
                            }
                        }

                    }
                    catch(Exception ex)
                    {
                        this.Foreground = Brushes.Red;
                        Keyboard.ClearFocus();
                        e.Handled = true;
                        IsSuccessSet = true;
                        Growl.Warning(ex.Message);
                    }
                }
            }
        }


        public static readonly DependencyProperty MaxValueProperty =
     DependencyProperty.Register(
         "MaxValue", typeof(double), typeof(TextBoxNumber), new PropertyMetadata(0.0));
        public double MaxValue
        {
            get { return (double)GetValue(MaxValueProperty); }
            set
            {
                SetValue(MaxValueProperty, value);
            }
        }

        public static readonly DependencyProperty MinValueProperty =
    DependencyProperty.Register(
    "MinValue", typeof(double), typeof(TextBoxNumber), new PropertyMetadata(0.0));
        public double MinValue
        {
            get { return (double)GetValue(MinValueProperty); }
            set
            {
                SetValue(MinValueProperty, value);
            }
        }

        /// <summary>
        /// 传入 "int" ,"double"
        /// </summary>
        public static readonly DependencyProperty ValueTypeProperty =
DependencyProperty.Register(
"ValueType", typeof(string), typeof(TextBoxNumber), new PropertyMetadata("int"));
        public string ValueType
        {
            get { return (string)GetValue(ValueTypeProperty); }
            set
            {
                SetValue(ValueTypeProperty, value);
            }
        }


    }
}
